package com.surpassun.emall.service.impl;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.surpassun.emall.model.Statistics;
import com.surpassun.emall.repository.StatisticsRepository;
import com.surpassun.emall.service.StatisticsService;
import com.surpassun.emall.util.Validator;

/**
 *
 * @author Denis
 */
@Service
public class StatisticsServiceImpl extends AbstractServiceImpl<Statistics> implements StatisticsService {

	@Inject
	StatisticsRepository statisticsRepository;
	
	public StatisticsServiceImpl() {
    	super(Statistics.class);
    }
	
	@Override
	public void addRecord(String field, long referenceId) {
		Statistics existed = this.getExisted(field, referenceId);
		if (Validator.isNotNull(existed)) {
			int total = existed.getTotal();
			existed.setTotal(total + 1);
			statisticsRepository.update(existed);
		} else {
			Statistics statistics = new Statistics(field, referenceId, 1);
			statisticsRepository.add(statistics);
		}
	}

	@Override
	public Statistics getExisted(String field, long referenceId) {
		return statisticsRepository.getExisted(field, referenceId);
	}

	@Override
	public int getTotal(String field, long referenceId) {
		return statisticsRepository.getTotal(field, referenceId);
	}

	@Override
	public List<Long> getTopItems(String field, int number) {
		return statisticsRepository.getTopItems(field, number);
	}

}